<?php

namespace Frieza\Modules\Position\Controllers;

use Biz\service\advertisement\apiModule\BroadcastWithPos;
use Doraemon\model\Circle\CircleBaseInfo;
use Doraemon\service\position\PositionConstInterface;
use Doraemon\service\position\PositionModule;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class BroadcastWithPosController extends BaseController
{
    use BaseTrait;

    public function saveModule(Request $request, $baseInfoId)
    {
        $base = $request->input('base', []);
        $rules = $request->input('rules', []);
        $pic = $base['pic_id'] ?? '';
        $pos = $request->input('base.pos', 0);

        switch ($this->positionId) {
            case PositionModule::CIRCLE_DETAIL_NOTICE:
            case PositionModule::USER_DETAIL_CAROUSEL_BANNER:
            case PositionModule::HOME_CAROUSEL_V5:
                if ($pos <= 0)  throw new \Exception('位置必选大于0');
                if (empty($pic)) throw new \Exception('图片必须上传');
                $id = $base['id'] ?? 0;
                $copy_id = $base['copy_id'] ?? 0;
                $img_size = $rules['img_size'] ?? 0;
                if (!(($id > 0 || $copy_id > 0) && $img_size == 0)) $this->rulesValid($rules, 0);
                break;
            default:
                break;
        }
    }
    public function beforeTitleForm(Form $form)
    {
        switch ($this->positionId) {
            case PositionConstInterface::EDITOR_COPY:
                $form->select('base.pos', '发布器类型')->options(BroadcastWithPos::EDITOR_COPY_PUBLISH_MAP);
                $form->textarea('base.pic_id', '文案')->help("最多可输入200字")->attribute('maxlength', 200);
                break;
        }
    }

    public function defaultBaseInfo(Request $request)
    {
        $response = parent::defaultBaseInfo($request);

        if ($this->positionId == PositionConstInterface::CIRCLE_DETAIL_NOTICE) {
            $response['pos'] = 1;

            $response['pic_id'] = '4c468301u01609m00r1dlmg';
        }

        return $response;
    }

    public function beforeLinkForm(Form $form)
    {
        switch ($this->positionId) {
            case PositionConstInterface::EDITOR_COPY:
                break;
            case PositionConstInterface::SEARCH_PRE_WORD:
                $form->text('base.title', '预置词')->required();
                $form->number('base.ext_type', '前几页');
                $form->number('base.pos', '第几位');
                break;
            default:
                $form->select('base.ext_type', '选择圈子')->options(CircleBaseInfo::getNameId())->required();
                $form->radioButton('base.pos', '消息位置')
                    ->options(array_combine(range(1, 5), range(1, 5)));
                $form->text('base.title', '消息文案');
                $this->bannerFormWithCheck($form, 'base.pic_id', $label = '文案图标');
                break;
        }
    }

    public function extendList(Grid $grid)
    {
        switch ($this->positionId) {
            case PositionConstInterface::EDITOR_COPY:
                $this->filter_field_pos = false;
                $this->filter_field_uid = false;
                $grid->column('pos', '发布器类型')->using(BroadcastWithPos::EDITOR_COPY_PUBLISH_MAP)->label([
                    BroadcastWithPos::NOTE_PUBLISH => 'warning',
                    BroadcastWithPos::DIARY_PUBLISH => 'info',
                ]);
                $grid->column('pic_id', '文案');
                break;
            case PositionConstInterface::SEARCH_PRE_WORD:
                $grid->column('title', '预置词');
                $grid->column('ext_type', '前几页');
                $grid->column('pos', '第几位');
                break;
            default:
                $grid->column('pos', '位置')->label([
                    1 => 'success',
                    2 => 'warning',
                    3 => 'success',
                    4 => 'info',
                    5 => 'default',
                ]);
                $grid->column('ext_type', '圈子')->using(CircleBaseInfo::getNameId());
                $grid->column('title', '消息文案');
                $grid->column('pic_url', '文案图标')->image("", 120);
                break;
        }

    }

}
